package com.lianzhihui.romeex.widget.keybord;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.KeyboardAdapter;
import com.lianzhihui.romeex.utils.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Copyright (C), 2019-2019, wase_five
 * FileName: SafetyKeyBoardView
 * Author: echo
 * Date: 2019/12/12 17:49
 * Description: 自定义安全键盘
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
public class SafetyKeyBoardView extends RelativeLayout implements BaseQuickAdapter.OnItemClickListener , TextWatcher {
    private SafetyKeyWordsListener safetyKeyWordsListener;
    private SafetyFinishInputListener safetyFinishInputListener;
    //随机变量键盘下标
    private final Integer[] INDEX_POSATION = {0,1,2,3,4,5,6,7,8,10};
    //随机变量键盘值
    private String[] INDEX_FULL_VALUE = {"1","2","3","4","5","6","7","8","9","","0","DEL"};
    private RecyclerView recyclerView;
    private KeyboardAdapter keyboardAdapter;
    private List<String> KeyValues;
    private View passwordView;
    private TextView tv_title;
    private TextView et_code_one;
    private TextView et_code_two;
    private TextView et_code_three;
    private TextView et_code_four;
    private TextView et_code_five;
    private TextView et_code_six;
    private View view_label_one;
    private View view_label_two;
    private View view_label_three;
    private View view_label_four;
    private View view_label_five;
    private View view_label_six;
    private TextView tv_code_error;
    private TextView tv_more;
    private View iv_close;
    private boolean hasPanel;

    public SafetyKeyBoardView(Context context) {
        super(context);
        initView(context);
    }
    public SafetyKeyBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    public SafetyKeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    public SafetyKeyBoardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }
    public void setOnKeyWordsListener(SafetyKeyWordsListener safetyKeyWordsListener){
        this.safetyKeyWordsListener = safetyKeyWordsListener;
    }
    public void setOnInputFinishListener(SafetyFinishInputListener safetyFinishInputListener){
        this.safetyFinishInputListener = safetyFinishInputListener;
    }
    private void initView(Context context){
        inflate(context, R.layout.view_safety_keyboard, this);
        recyclerView = findViewById(R.id.mRecyclerView);
        passwordView = findViewById(R.id.ll_password_view);
        tv_title = findViewById(R.id.tv_title);
        et_code_one = findViewById(R.id.et_code_one);
        et_code_two = findViewById(R.id.et_code_two);
        et_code_three = findViewById(R.id.et_code_three);
        et_code_four = findViewById(R.id.et_code_four);
        et_code_five = findViewById(R.id.et_code_five);
        et_code_six = findViewById(R.id.et_code_six);
        view_label_one = findViewById(R.id.view_label_one);
        view_label_two = findViewById(R.id.view_label_two);
        view_label_three = findViewById(R.id.view_label_three);
        view_label_four = findViewById(R.id.view_label_four);
        view_label_five = findViewById(R.id.view_label_five);
        view_label_six = findViewById(R.id.view_label_six);
        et_code_one.addTextChangedListener(this);
        et_code_two.addTextChangedListener(this);
        et_code_three.addTextChangedListener(this);
        et_code_four.addTextChangedListener(this);
        et_code_five.addTextChangedListener(this);
        et_code_six.addTextChangedListener(this);
        tv_code_error = findViewById(R.id.tv_code_error);
        iv_close = findViewById(R.id.iv_close);
        tv_more = findViewById(R.id.tv_more);
        init();
    }

    public void setTitle(String title){
        tv_title.setText(title);
    }
    /**
     * 是否展示密码输入面板
     * @param show
     */
    public void showPasswodPanel(boolean show){
        hasPanel = show;
        if (show){
            passwordView.setVisibility(VISIBLE);
            cleanFocsable();
            et_code_one.setFocusable(true);
        }else {
            passwordView.setVisibility(GONE);
        }
    }

    public void showToolbarMore(boolean show){
        if (show){
            tv_more.setVisibility(VISIBLE);
        }else {
            tv_more.setVisibility(GONE);
        }
    }

    public void showError(boolean show){
        if (show){
            view_label_one.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.red));
            view_label_two.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.red));
            view_label_three.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.red));
            view_label_four.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.red));
            view_label_five.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.red));
            view_label_six.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.red));
            tv_code_error.setVisibility(VISIBLE);
        }else {
            view_label_one.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            view_label_two.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            view_label_three.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            view_label_four.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            view_label_five.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            tv_code_error.setVisibility(INVISIBLE);
        }
    }

    public void setCloseAction(OnClickListener onClickListener){
        iv_close.setOnClickListener(onClickListener);
    }
    public void setMoreAction(OnClickListener onClickListener){
        tv_more.setOnClickListener(onClickListener);
    }
    public void init(){
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3,1,false));
        keyboardAdapter = new KeyboardAdapter(new ArrayList<>());
        keyboardAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(keyboardAdapter);
        if (hasPanel){
            cleanFocsable();
            et_code_one.setFocusable(true);
        }
        shuffleIndex();
    }

    public void shuffleIndex(){
        List<Integer> list = Arrays.asList(INDEX_POSATION);
        Collections.shuffle(list);
        for (int i = 0; i < list.size(); i++) {
            INDEX_FULL_VALUE[list.get(i)] = i+"";
        }
        KeyValues = Arrays.asList(INDEX_FULL_VALUE);
        keyboardAdapter.setNewData(KeyValues);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (position==11){
            if (hasPanel){
                onDeleteWord();
            }else {
                safetyKeyWordsListener.onDeleteWord();
            }
        }
        if (position!=9&&position!=11){
            if (hasPanel){
                onResultWord(KeyValues.get(position));
            }else {
                safetyKeyWordsListener.onResultWord(KeyValues.get(position));
            }
        }
    }
    private void cleanInput(){
        et_code_one.setText("");
        et_code_one.setFocusable(false);
        et_code_two.setText("");
        et_code_two.setFocusable(false);
        et_code_three.setText("");
        et_code_three.setFocusable(false);
        et_code_four.setText("");
        et_code_four.setFocusable(false);
        et_code_five.setText("");
        et_code_five.setFocusable(false);
        et_code_six.setText("");
        et_code_six.setFocusable(false);
        et_code_one.setFocusable(true);
        view_label_one.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
        view_label_two.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
        view_label_three.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
        view_label_four.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
        view_label_five.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
        view_label_six.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
    }
    private void cleanFocsable(){
        et_code_one.setFocusable(false);
        et_code_two.setFocusable(false);
        et_code_three.setFocusable(false);
        et_code_four.setFocusable(false);
        et_code_five.setFocusable(false);
        et_code_six.setFocusable(false);
    }
    public void onResultWord(String word) {
        if (et_code_one.hasFocusable()){
            et_code_one.setText(word);
            view_label_one.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            return;
        }
        if (et_code_two.hasFocusable()){
            et_code_two.setText(word);
            view_label_two.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            return;
        }
        if (et_code_three.hasFocusable()){
            et_code_three.setText(word);
            view_label_three.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            return;
        }
        if (et_code_four.hasFocusable()){
            et_code_four.setText(word);
            view_label_four.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            return;
        }
        if (et_code_five.hasFocusable()){
            et_code_five.setText(word);
            view_label_five.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorText));
            return;
        }
        if (et_code_six.hasFocusable()){
            et_code_six.setText(word);
//            view_label_six.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorAccent));
            return;
        }
    }
    public void onDeleteWord() {
        if (et_code_one.hasFocusable()){
            et_code_one.setText("");
            view_label_one.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
            cleanFocsable();
            et_code_one.setFocusable(true);
        }
        if (et_code_two.hasFocusable()){
            et_code_two.setText("");
            view_label_two.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
            cleanFocsable();
            et_code_one.setFocusable(true);
        }
        if (et_code_three.hasFocusable()){
            et_code_three.setText("");
            view_label_three.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
            cleanFocsable();
            et_code_two.setFocusable(true);
        }
        if (et_code_four.hasFocusable()){
            et_code_four.setText("");
            view_label_four.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
            cleanFocsable();
            et_code_three.setFocusable(true);
        }
        if (et_code_five.hasFocusable()){
            et_code_five.setText("");
            view_label_five.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
            cleanFocsable();
            et_code_four.setFocusable(true);
        }
        if (et_code_six.hasFocusable()){
            et_code_six.setText("");
            view_label_six.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_password_label));
            cleanFocsable();
            et_code_five.setFocusable(true);
            showError(false);
        }
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (et_code_one.getText().length()==1){
            et_code_one.setFocusable(false);
            et_code_two.setFocusable(true);
        }
        if (et_code_two.getText().length()==1){
            et_code_two.setFocusable(false);
            et_code_three.setFocusable(true);
        }
        if (et_code_three.getText().length()==1){
            et_code_three.setFocusable(false);
            et_code_four.setFocusable(true);
        }
        if (et_code_four.getText().length()==1){
            et_code_four.setFocusable(false);
            et_code_five.setFocusable(true);
        }
        if (et_code_five.getText().length()==1){
            et_code_five.setFocusable(false);
            et_code_six.setFocusable(true);
        }
        if (et_code_six.getText().length()==1){
            String code = et_code_one.getText().toString()+
                    et_code_two.getText().toString()+
                    et_code_three.getText().toString()+
                    et_code_four.getText().toString()+
                    et_code_five.getText().toString()+
                    et_code_six.getText().toString();
            if (safetyFinishInputListener!=null)
                safetyFinishInputListener.onFinishInput(code);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
